package com.edu.edumanager.service.imp;

import com.edu.edumanager.bean.EduCourse;
import com.edu.edumanager.bean.EducourseDes;
import com.edu.edumanager.exception.EduException;
import com.edu.edumanager.mapper.EduCourseDesMapper;
import com.edu.edumanager.mapper.EduCourseMapper;
import com.edu.edumanager.service.ChapterService;
import com.edu.edumanager.service.ClassService;
import com.edu.edumanager.service.CourseService;
import com.edu.edumanager.service.VideoService;
import com.edu.edumanager.vo.EduCourseInfo;
import com.edu.edumanager.vo.EduCourseVo;
import com.edu.edumanager.vo.EduResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.security.auth.callback.Callback;
import java.util.Date;
import java.util.List;

/**
 * @program: edu-manager
 * @description:
 * @author: Jaime
 * @create: 2021-08-18 14:14
 **/
@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    EduCourseMapper eduCourseMapper;
    @Autowired
    EduCourseDesMapper eduCourseDesMapper;
    @Autowired
    ChapterService chapterService;
    @Autowired
    VideoService videoService;
    @Autowired
    ClassService classService;

    @Override
    @Transactional(rollbackFor =Exception.class)
    public int addCourse(EduCourseInfo courseInfo) {
        EduCourse course = courseInfo.getCourse();
        course.setStatus("Draft");
        Date date = new Date();
        course.setGmtModified(date);
        course.setGmtCreated(date);
        int i = eduCourseMapper.addCourse(course);
        if (i == 0) {
            throw new EduException("添加课程失败");
        }

        EducourseDes educourseDes = new EducourseDes();
        educourseDes.setId(course.getId());
        educourseDes.setDes(courseInfo.getDes());
        educourseDes.setGmtCreate(date);
        educourseDes.setGmtModified(date);
        int j = eduCourseDesMapper.addCourseDes(educourseDes);
        if (j == 0) {
            throw new EduException("添加课程失败");
        }
        return course.getId();
    }

    @Override
    public EduCourseInfo findCourseInfoById(Integer courseId) {
        EduCourse course =  eduCourseMapper.findCourseById(courseId);
        EducourseDes educourseDes = eduCourseDesMapper.findCourseDesById(courseId);
        if (course==null&educourseDes==null ){
            throw new EduException("查询课程信息失败");
        }
        EduCourseInfo courseInfo = new EduCourseInfo();
        courseInfo.setCourse(course);
        courseInfo.setDes(educourseDes.getDes());
        return courseInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateCourseInfo(EduCourseInfo courseInfo) {
        EduCourse course = courseInfo.getCourse();
        course.setStatus("Draft");
        Date date = new Date();
        course.setGmtModified(date);
        int i = eduCourseMapper.updateCourse(course);
        if (i == 0) {
            throw new EduException("添加课程失败");
        }

        EducourseDes educourseDes = new EducourseDes();
        educourseDes.setId(course.getId());
        educourseDes.setDes(courseInfo.getDes());
        educourseDes.setGmtModified(date);
        int j = eduCourseDesMapper.updateCourseDes(educourseDes);
        if (j == 0) {
            throw new EduException("添加课程失败");
        }
        return course.getId();
    }

    @Override
    public EduCourseVo getCourseTeacher(Integer courseId) {
        EduCourseVo vo = new EduCourseVo();
        EduCourse course = eduCourseMapper.getCourseTeacher(courseId);
        System.out.println(course);
        //查询一级类目
        String subjectLevelOne = classService.findSubjectTitleById(course.getSubjectId());
        vo.setSubjectLevelOne(subjectLevelOne);
        String subjectLevelTwo = classService.findSubjectTitleById(course.getSubjectParentId());
        vo.setSubjectLevelTwo(subjectLevelTwo);
        vo.setCourseId(course.getId());
        vo.setTitle(course.getTitle());
        vo.setCover(course.getCover());
        vo.setLessonNum(course.getLessonNum());
        vo.setPrice(course.getPrice());
        vo.setTeacherName(course.getEduTeacher().getName());
        return vo;
    }

    @Override
    public int publishCourse(Integer courseId) {
        int i = eduCourseMapper.publishCourse(courseId);
        if(i==0){
            throw new EduException("发布课程失败");
        }
        return i;
    }

    @Override
    public EduResult conditionsQuery(Integer currentPage, Integer limit) {
        List<EduCourseVo> row = eduCourseMapper.findCourseByPage((currentPage-1)*limit,limit);
        int total = eduCourseMapper.findCourseTotal();
        return EduResult.ok().data("row",row).data("total",total);
    }
    @Transactional(rollbackFor = EduException.class)
    @Override
    public int deleteCourse(Integer courseId) {
        int a = videoService.deleteVideoByCourseId(courseId);
        int b = chapterService.deleteChapterByCourseId(courseId);
        int i = eduCourseMapper.deleteCourse(courseId);
        if(i==0){
            throw new EduException("删除课程失败");
        }
        return i;
    }
}
